// JavaScript Document

function MenuStatus(lgc)
{
	var _this = this;
	LogicStatus.call(_this, lgc)
	
	// 做个示范. 比如我画开始菜单的bg
	_this.bg = undefined;
	_this.button = undefined;
	//_this.musicButton = undefined;
	_this.dango = undefined;
	_this.title_1 = undefined;
	_this.title_2 = undefined;
    _this.emptyBox = new PhysicsNode(new Vector2(360, 270), new Rectangle(100, 100), new Vector2(0, 0), new Vector2(0, 0));
    
	_this.prepare = function(preStat){
	    if(preStat == OVER || preStat == PAUSE)
	    {
	        //alert("重新开始游戏了, 骚年");
	        init();
	    }
	}
	
	_this.update = function(){
		// 在这里实现物件的更新, 
		_this.bg.position.y += 0.5;
		
		_this.physics.updateObject(_this.dango.physicsNode, 0.5);
		
		if(_this.physics.detectCollision(_this.dango.physicsNode, _this.emptyBox))
		{
		    _this.dango.physicsNode.velocity.x = -_this.dango.physicsNode.velocity.x;
		    _this.dango.physicsNode.velocity.y = -_this.dango.physicsNode.velocity.y;
		}
		_this.dango.update();
	}
	
	_this.mouseClick = function(e){
	    var CanvasEle = document.getElementById("myCanvas");
	    //alert(distanceCanvas.offsetLeft);
	    var mouseX = e.clientX - CanvasEle.offsetLeft;
	    var mouseY = e.clientY - CanvasEle.offsetTop;
	    //alert(mouseX);
	    if(mouseX > _this.button.position.x && mouseX < (_this.button.position.x+_this.button.box.width) && mouseY > _this.button.position.y && mouseY < (_this.button.position.y+_this.button.box.height))
		   {
		       _this.clearUp();
               _this.logic.statusChange(GAMING);
               //_this.logic.statusChange(OVER);
		   }
		   		
	}
	
  _this.mouseMove = function(e)
     {
        var CanvasEle = document.getElementById("myCanvas");
        var mouseX = e.clientX - CanvasEle.offsetLeft;
        var mouseY = e.clientY - CanvasEle.offsetTop;
        //alert(mouseX);
        if (mouseX > _this.button.position.x && mouseX < (_this.button.position.x+_this.button.box.width) && mouseY > _this.button.position.y && mouseY < (_this.button.position.y+_this.button.box.height))
        {
           _this.button.changeIn();
         }
        if (mouseX < _this.button.position.x || mouseX > (_this.button.position.x+_this.button.box.width) || mouseY < _this.button.position.y || mouseY > (_this.button.position.y+_this.button.box.height))
         {
            _this.button.changeOut();
         }
     }
     
	_this.keyDown = function(e){
		// 比如我按键之后转换为状态游戏 : 就这么做:
		// 1. 先清理自己的资源
		// 2. 将游戏状态转换为Gaming
		//_this.logic.statusChange(OVER);
		
	}
	
	// 这个函数用来清理资源
	_this.clearUp = function(){
		_this.logic.sceneManager.clearNodeList();
	}
	// 这个函数用来查询ready状态.
	_this.ready = function(){
		return _this.bg.ready && _this.button.ready;
	}
	
	// 做个示范. 假设我的menu界面背景是往下滚动的.
	var init = function () {
	    _this.physics = new Physics();
	    
		_this.bg = new Bg(new Vector2(0,0), new Rectangle(480, 768));
		_this.bg.img.src = "./images/loopBG.png";
		var sNode = _this.logic.sceneManager.createNode( _this.bg, 4);	
		_this.logic.sceneManager.addNode(sNode);
        
        _this.title_1 = new ChangeButton(new Vector2(55, 126), new Rectangle(0, 0));
        _this.title_1.img.src = "./images/title1.png";
        sNode = _this.logic.sceneManager.createNode( _this.title_1, 1);  
        _this.logic.sceneManager.addNode(sNode);
        
        _this.title_2 = new ChangeButton(new Vector2(242, 254), new Rectangle(0, 0));
        _this.title_2.img.src = "./images/title2.png";
        var sNode = _this.logic.sceneManager.createNode( _this.title_2, 1);  
        _this.logic.sceneManager.addNode(sNode);
        
		_this.button = new ChangeButton(new Vector2(112, 434), new Rectangle(256, 97));
		_this.button.img.src = "./images/Start.png";
		_this.button.src_1 = "./images/Start.png";
		_this.button.src_2 = "./images/Start_h.png";
		sNode = _this.logic.sceneManager.createNode(_this.button, 1);
		_this.logic.sceneManager.addNode(sNode);
				
		
		_this.dango = new Dango(new Vector2(365, 210), new Rectangle(56, 47));
		_this.dango.physicsNode.velocity = new Vector2(0, -15);
		_this.dango.physicsNode.acceleration = new Vector2(0, 1);
		sNode = _this.logic.sceneManager.createNode(_this.dango, 1);
        _this.logic.sceneManager.addNode(sNode);
	}
	
	init();
}